﻿using Nex_CSharp_Demo.utils;
using NexC_Sharp.Services;
using RobotPerCeption;
using System.Windows.Controls;

namespace Nex_CSharp_Demo.ViewModel
{
    public class ThreadData
    {


        CancellationTokenSource cts = null;
        public static bool tag { set; get; } //停止进程

        public double[] position = new double[7];
        public AutoResetEvent exitEvent;
        CancellationToken token { set; get; }

        public static int thread_manager_id { set; get; }

        private Thread thread { get; set; }
        private int waitTime;


        public Thread GetThread()
        {
            return this.thread;
        }


        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="time">挂机时间m/s</param>
        /// <param name="coord">当前机器人坐标系</param>
        public ThreadData(int time, int coord
            , TextBox x,
            TextBox y,
            TextBox z,
            TextBox a,
            TextBox b,
            TextBox c,
            int positionIndex
            )
        {
            cts = new CancellationTokenSource();
            exitEvent = new AutoResetEvent(false);
            waitTime = time;
            token = cts.Token;
            Console.WriteLine("struct function token hash" + token.GetHashCode());

            thread = new Thread(() => Process(cts, coord, x, y, z, a, b, c, positionIndex));

            thread_manager_id = this.thread.ManagedThreadId;
        }
        public void Run(int position)
        {

            //设置机器人坐标系  坐标系 0：关节 1：直角 2：工具 3：用户

            Console.WriteLine("run function token hash" + this.cts.Token.GetHashCode());
            ThreadUtils.addThread(position, this.thread, token, cts);
            thread.Start();


        }


        /// <summary>
        /// 开关调用 
        /// </summary>
        /// <param name="token">token</param>
        /// <param name="position">机器人的坐标系</param>

        private void Process(CancellationTokenSource token, int coord
            , TextBox x,
            TextBox y,
            TextBox z,
            TextBox a,
            TextBox b,
            TextBox c, int positionIndex
            )
        {



            //Console.Write("x:" + x.Text + "y:" + y.Text + "z:" + z.Text + "a:" + a.Text + "b:" + b.Text + "c:" + c.Text);
            while (!token.IsCancellationRequested)
            {
                Nex.get_current_position(position, coord, MainWindow.roboteName);

                //0：关节 1：直角 2：工具 3：用户
                //关节坐标系 全改   其他模式 改 a b c



                x.Dispatcher.BeginInvoke(new Action(() => { x.Text = position[0] + ""; }));
                y.Dispatcher.BeginInvoke(new Action(() => { y.Text = position[1] + ""; }));
                z.Dispatcher.BeginInvoke(new Action(() => { z.Text = position[2] + ""; }));
                a.Dispatcher.BeginInvoke(new Action(() => { a.Text = position[3] + ""; }));
                b.Dispatcher.BeginInvoke(new Action(() => { b.Text = position[4] + ""; }));
                c.Dispatcher.BeginInvoke(new Action(() => { c.Text = position[5] + ""; }));

                /*   MainWindow.position = null;
                   MainWindow.position = position;*/
                Thread.Sleep(500);

            }
        }



    }
}
